/*
package com.qf.filter;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.User;
import com.qf.utils.SYS;
import com.qf.vo.MsgResult;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebFilter(urlPatterns = {"/cart","/order"})
public class logInFilter implements Filter{

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //login.jsp/放行????
        //是否已经登录
        //获得session并取出用户信息
        HttpServletRequest req=(HttpServletRequest)request;
        HttpServletResponse resp=(HttpServletResponse)response;
        HttpSession session=req.getSession();
        Object obj=session.getAttribute(SYS.USER);
        if(obj!=null&&obj instanceof User){
            User user=(User)obj;
            //放行
            chain.doFilter(request, response);
            System.out.println("logged,fangxing...");
        }else {
            System.out.println("lanjie");
            String s=req.getHeader("xhr");
            if(s!=null&&s.equals("ajax")){
                PrintWriter pw=resp.getWriter();
                MsgResult msg=new MsgResult();
                msg.setCode(-1);
                msg.setMsg("logIn.jsp");
                String str= JSON.toJSONString(msg);
                pw.write(str);
                pw.flush();
                pw.close();
            } else{
                resp.sendRedirect("logIn.jsp");
            }
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}

*/
